package com.zmn.oms.business.impl.performance;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.business.interfaces.performance.PerformanceUtilService;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.serviceitem.OrderServiceItem;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.serviceitem.OrderServiceItemService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName PerformanceUtilServiceImpl
 * @description:
 * @author: wangyanci
 * @create: 2021-04-23 17:22
 * @Version 1.0
 **/
@Service
@Slf4j
public class PerformanceUtilServiceImpl implements PerformanceUtilService {

    @Autowired
    private OrderServiceItemService orderServiceItemService;
    @Autowired
    private OrderWorkService orderWorkService;

    @Override
    public Integer conversionPerformanceAmount(Long orderId, BigDecimal businessField, Integer businessType) {
        log.info("PerformanceUtilServiceImpl.conversionPerformanceAmount orderId={},businessField={},businessType={}", orderId, businessField, businessType);
        if (Objects.equals(AMOUNT_TYPE,businessType)) {
            return businessField.intValue();
        }
        // （内部价-配件费）*比例
//        try {
//            return (internalTotalPrice(orderId) - partAmount(orderId)) * businessField.intValue();
//
//        } catch (Exception e) {
//            log.error("PerformanceUtilServiceImpl.conversionPerformanceAmount error:{}", e.getMessage());
//        }
        return null;
    }

    /** @Author wangyanci
     * @Description 订单内部价（单位：分）
     * @Date 17:30 2021/4/23
     * @Param [orderId]
     * @return java.lang.Integer
     **/
    private Integer internalTotalPrice(Long orderId) throws OmsBaseException {

        log.info("PerformanceUtilServiceImpl.internalTotalPrice orderId={}", orderId);
        if (NumberUtil.isNullOrZero(orderId)) {
            throw new OmsBaseException("orderId is null!");
        }
        List<OrderServiceItem> orderServiceItems = orderServiceItemService.listByOrderId(orderId);

        return orderServiceItems.stream().mapToInt(OrderServiceItem::getInternalTotalPrice).sum();
    }

    /** @Author wangyanci
     * @Description 配件金额
     * @Date 17:34 2021/4/23
     * @Param [orderId]
     * @return java.lang.Integer
     **/
    private Integer partAmount(Long orderId) throws OmsBaseException {
        log.info("PerformanceUtilServiceImpl.partAmount orderId={}", orderId);
        if (NumberUtil.isNullOrZero(orderId)) {
            throw new OmsBaseException("orderId is null!");
        }
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, orderId);
        if (Objects.isNull(orderWork)) {
            throw new OmsBaseException("orderWork is null!");
        }

        return orderWork.getPartAmount();
    }

}